package com.raptor.trustworthy.task.controller;

import com.raptor.trustworthy.task.model.ScheduleJob;
import com.raptor.trustworthy.task.service.ScheduleJobService;
import java.util.HashMap;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 定时任务控制
 *
 * @Author renhuan
 * @Date 2021-03-27 15:24
 **/
@RestController
@RequestMapping("/scheduleJobControl")
public class ScheduleJobController {

  private final static Logger log = LoggerFactory.getLogger(ScheduleJobController.class);

  @Autowired
  private ScheduleJobService scheduleJobService;


  /**
   * 查询所有定时任务
   */
  @PostMapping("/getScheduleJob")
  public Map<String, Object> getScheduleJob() {
    Map<String, Object> res = scheduleJobService.getScheduleJob();
    return res;
  }

  /**
   * 分页查询定时任务
   *
   * @param 'curPage' 当前页
   * @param 'pageSize' 总页数
   */
  @PostMapping("/getScheduleJobListByPage")
  public Map<String, Object> getScheduleJobListByPage(@RequestBody Map<String, Object> param) {
    Map<String, Object> res = scheduleJobService.getScheduleJobListByPage(param);
    return res;
  }

  /**
   * 添加定时任务信息
   */
  @PostMapping("/insertScheduleJob")
  public Map<String, Object> insertScheduleJob(@RequestBody ScheduleJob scheduleJob) {

    Map<String, Object> res = scheduleJobService.insertScheduleJob(scheduleJob);
    if ("1".equals(scheduleJob.getState())){
      try {
        scheduleJobService.startJob(scheduleJob);
      } catch (Exception e) {
        log.error("scheduleJobService.startJob,jobName: " + scheduleJob.getJobName() + "异常： " + e);
      }
    }
    return res;
  }

  /**
   * 删除定时任务信息
   */
  @PostMapping("/deleteScheduleJob")
  public Map<String, Object> deleteScheduleJob(@RequestBody ScheduleJob ScheduleJob) {
    Map<String, Object> res = scheduleJobService.deleteScheduleJob(ScheduleJob);
    return res;
  }

  /**
   * 更改定时任务信息
   */
  @PostMapping("/updateScheduleJob")
  public Map<String, Object> updateScheduleJob(@RequestBody ScheduleJob ScheduleJob) {

    Map<String, Object> res = scheduleJobService.updateScheduleJob(ScheduleJob);
    return res;
  }



  /**
   * 启动定时任务
   */
  @PostMapping("/startJob")
  public Map<String, Object> startJob(@RequestBody ScheduleJob job) {
    ScheduleJob scheduleJobStart = scheduleJobService.getScheduleJobById(job);
    Map<String, Object> res = new HashMap<>();
    if (scheduleJobStart != null) {
      res = scheduleJobService.startJob(scheduleJobStart);
    } else {
      log.error("startJob ==> scheduleJobService.getScheduleJobById为null,id: " + job.getId());
      res.put("resCode","1");
      res.put("resMsg","startJob ==> scheduleJobService.getScheduleJobById为null,id: " + job.getId());
    }
    return res;
  }

  /**
   * 停止定时任务
   */
  @PostMapping("/pauseJob")
  public Map<String, Object> pauseJob(@RequestBody ScheduleJob job) {

    Map<String, Object> res = scheduleJobService.pauseJob(job);
    ScheduleJob scheduleJobUpdate = new ScheduleJob();
    scheduleJobUpdate.setId(job.getId());
    scheduleJobUpdate.setState("0");
    scheduleJobService.updateScheduleJob(scheduleJobUpdate);
    return res;
  }

}
